class StockTradesController < ApplicationController
  def cal_daily
    stocks = Stock.find(:all, :conditions=>["id=?", 1], :order=>"id")
    stocks.each do |s|
#      if s.id>1
        sts = StockTrade.find(:all, :conditions=>["stock_id=?", s.id], :order=>"trade_date")
        sts.each_with_index do |st,i|
          if i>=1
            today_price = st.price
            yesterday_price = sts[i-1].price
            if today_price
              if yesterday_price
                begin
                  daily_return = (today_price - yesterday_price)/yesterday_price
                  st.daily_return = daily_return
                  st.save
                rescue
                end
              end
            end
          end
        end
#      end
    end
    render :text=>"done"
  end
end
